﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using System.Windows.Controls.Primitives;
using for7raid.vkPlayer;
//using Microsoft.Expression.Interactivity.Core;

namespace for7raid.Control
{
    public class SliderPreviewBehavior : Behavior<Slider>
    {

        public static TimeConverter converter = new TimeConverter();
       
        public static Style GetContentStyle(Slider slider)
        {
            return (Style)slider.GetValue(ContentStyleProperty);
        }

        public static void SetContentStyle(
          Slider slider, Style value)
        {
            slider.SetValue(ContentStyleProperty, value);
        }

        public static readonly DependencyProperty ContentStyleProperty =
            DependencyProperty.RegisterAttached(
            "ContentStyle",
            typeof(Style),
            typeof(SliderPreviewBehavior),
            new UIPropertyMetadata(null));
       

        
        private static ContentAdorner GetPopupAdorner(Slider slider)
        {
            return (ContentAdorner)slider.GetValue(PopupAdornerProperty);
        }

        private static readonly DependencyProperty PopupAdornerProperty = DependencyProperty.RegisterAttached(
            "PopupAdorner",
            typeof(ContentAdorner),
            typeof(SliderPreviewBehavior),
            new UIPropertyMetadata(null));

        public SliderPreviewBehavior()
        {
            
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.PreviewMouseMove += new System.Windows.Input.MouseEventHandler(item_PreviewMouseMove);
            AssociatedObject.MouseLeave += new System.Windows.Input.MouseEventHandler(item_MouseLeave);

            // Insert code that you would want run when the Behavior is attached to an object.
        }

        void AssociatedObject_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.PreviewMouseMove -= new System.Windows.Input.MouseEventHandler(item_PreviewMouseMove);
            AssociatedObject.MouseLeave -= new System.Windows.Input.MouseEventHandler(item_MouseLeave);


            // Insert code that you would want run when the Behavior is removed from an object.
        }

        static void item_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Slider slider = sender as Slider;
            ContentAdorner popup = GetPopupAdorner(slider);
            popup.Visibility = Visibility.Collapsed;
        }

        static void item_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Slider slider = sender as Slider;


            ContentAdorner popup = GetPopupAdorner(slider);
            if (popup == null)
            {
                Style style = GetContentStyle(slider);
                popup = new ContentAdorner(slider, style);
                slider.SetValue(PopupAdornerProperty, popup);
                AdornerLayer layer = AdornerLayer.GetAdornerLayer(slider);
                layer.Add(popup);
            }

            popup.Visibility = Visibility.Visible;

            Track _track = slider.Template.FindName("PART_Track", slider) as Track;

            Point position = e.MouseDevice.GetPosition(_track);

            double value = _track.ValueFromPoint(position);

            if (slider.SmallChange != 0.0)
            {
                double diff = value % slider.SmallChange;
                value -= diff;
            }
            value = Math.Max(slider.Minimum, Math.Min(slider.Maximum, value));
            popup.Content = converter.Convert(value, null, null, null);

            position = e.GetPosition(slider);
            position.Y = slider.ActualHeight / 2.0;
            popup.PlacementOffset = position;
        }

        
    }


    public class ContentAdorner : Adorner
    {
        public ContentAdorner(UIElement adornedElem, Style contentStyle)
            : base(adornedElem)
        {
            _contentControl = new ContentControl();
            _contentControl.Style = contentStyle;
            AddLogicalChild(_contentControl);
            AddVisualChild(_contentControl);
        }

        public object Content
        {
            set
            {
                _contentControl.Content = value;
            }
        }

        public Point PlacementOffset
        {
            get { return (Point)GetValue(PlacementOffsetProperty); }
            set { SetValue(PlacementOffsetProperty, value); }
        }

        public static readonly DependencyProperty PlacementOffsetProperty =
            DependencyProperty.Register("PlacementOffset", typeof(Point), typeof(ContentAdorner),
            new FrameworkPropertyMetadata(new Point(), FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure));


        protected override Size ArrangeOverride(Size finalSize)
        {
            if (_contentControl != null)
            {
                _contentControl.Arrange(new Rect(PlacementOffset, _contentControl.DesiredSize));
            }
            return finalSize;
        }

        protected override int VisualChildrenCount { get { return 1; } }
        protected override Visual GetVisualChild(int index) { return _contentControl; }


        private ContentControl _contentControl;
    }
}